package org.wattdepot.cli;

import java.util.Locale;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.summary.jaxb.SourceSummary;

/**
 * Executes the summary (of a source) command.
 * 
 * @author Scott Wong and Kelli Sawai
 * 
 */
public class SrcSummary extends CommandLineInterface implements WattDepotCliCommand {
  /** Explains the error. */
  public static final String resourceExceptionMessage = "Error: Resource not found";

  /** Explains the error. */
  public static final String exceptionMessage =
      "An error occured while executing the requested command.";

  /** Newline. */
  private String nl = "\n";

  /** Default for variable. */
  private String none = "none";

  /**
   * Executes and returns the results of the summary {source} command.
   * 
   * @param args The arguments required to execute this method.
   * @return The results of the command.
   */
  public String doCommand(String args) {
    try {
      if (args.matches("summary \\w*")) {
        // Finds what plant to look up.
        String[] input = args.split(" ");
        String sourceName = input[1].toUpperCase(Locale.ENGLISH);
        return listSummary(sourceName);
      }
      else {
        return "The command you have entered is invalid.";
      }
    }
    catch (WattDepotClientException e) {
      return "An error occured while executing the requested command.";
    }
    catch (Exception e) {
      return "An error occured while executing the requested command.";
    }
  }

  /**
   * Prints out a summary of the requested location.
   * 
   * @param name the name of the plant.
   * @throws WattDepotClientException If problems occur getting the source index.
   * @throws ResourceNotFoundException If resource not found.
   * @return A summary of the selected source.
   */
  private String listSummary(String name) throws WattDepotClientException,
      ResourceNotFoundException {
    try {
      StringBuffer buffer = new StringBuffer(200);
      Source source = client.getSource(name);
      SourceSummary summary = client.getSourceSummary(name);

      // Sources subSources
      String subSources = none;
      if (!(source.getSubSources() == null)) {
        subSources = source.getSubSources().toString();
        subSources = subSources.replace("[", "");
        subSources =
            subSources.replace("http://server.wattdepot.org:8182/wattdepot/sources/", "");
        subSources = subSources.replace("]", "");
      }

      // Sources owner
      String owner = source.getOwner();
      owner = owner.replace("http://server.wattdepot.org:8182/wattdepot/users/", "");

      // Sources properties
      String properties = none;
      if (!(source.getProperties() == null)) {
        properties = formatProperties(source.getProperties());  
      }

      // Sources first sensor data
      String firstSensor = none;
      if (!(summary.getFirstSensorData() == null)) {
        firstSensor = summary.getFirstSensorData().toString();
      }

      // Sources last sensor data
      String lastSensor = "none";
      if (!(summary.getLastSensorData() == null)) {
        lastSensor = summary.getLastSensorData().toString();
      }

      // Stores output to buffer.
      buffer.append("SubSources: ").append(subSources).append(nl);
      buffer.append("Description: ").append(source.getDescription()).append(nl);
      buffer.append("Owner: ").append(owner).append(nl);
      buffer.append("Location: ").append(source.getLocation()).append(nl);
      buffer.append("Coordinates: ").append(source.getCoordinates()).append(nl);
      buffer.append("Properties: ").append(properties).append(nl);
      buffer.append("Earliest data: ").append(firstSensor).append(nl);
      buffer.append("Latest data: ").append(lastSensor).append(nl);
      buffer.append("Total data points: ").append(summary.getTotalSensorDatas()).append(
          nl);
      return buffer.toString();
    }
    catch (ResourceNotFoundException e) {
      return resourceExceptionMessage;
    }
    catch (WattDepotClientException e) {
      return exceptionMessage;
    }
  }

}
